﻿using eLoan.Models;
using System;
using System.Linq;
using System.Web.Mvc;

namespace eLoan.Controllers
{
    public class AgentController : BaseController
    {
        public ActionResult AddEdit(int? id, string branchCode)
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Agents);

                var db = new DatabaseContext();

                var agentModel = AdministrationHelper.GetAgentModel(db, 0);
                agentModel.Branches = AdministrationHelper.GetBranchDropDownListByUserRole(db, User.Identity.Name);

                if (id == null)
                {
                    agentModel.DBCommand = AdministrationHelper.DBCommand.Add.ToString();
                    agentModel.Agent = new Agent();
                    agentModel.Agent.AgentName = string.Empty;
                    agentModel.Agent.Address = string.Empty;
                    agentModel.Agent.ContactNumbers = string.Empty;
                    agentModel.Agent.BranchCode = branchCode;
                    agentModel.Agent.Guarantor = string.Empty;
                    agentModel.Agent.GuarantorContactNumbers = string.Empty;
                    agentModel.Agent.IsActive = true;
                    
                    return View("AddEdit", agentModel);
                }
                else
                {
                    agentModel.DBCommand = AdministrationHelper.DBCommand.Edit.ToString();

                    var agents = db.Agents;
                    var agent = agents.Where(w => w.ID == id).SingleOrDefault<Agent>();
                    agentModel.Agent = agent;

                    return View("AddEdit", agentModel);
                }
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        [HttpPost]
        public ActionResult Save(Agent agent, string command)
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Agents);

                if (ModelState.IsValid)
                {
                    using (var db = new DatabaseContext())
                    {
                        if (command == AdministrationHelper.DBCommand.Add.ToString())
                        {
                            db.Agents.Add(agent);
                            db.SaveChanges();
                        }
                        else if (command == AdministrationHelper.DBCommand.Edit.ToString())
                        {
                            db.Entry(agent).State = System.Data.EntityState.Modified;
                            db.SaveChanges();
                        }
                        else
                        {
                            // cancelled
                        }
                    }
                }

                return RedirectToAction("Agents", "Administration", new { branchCode = agent.BranchCode });
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }
    }
}
